package com.fzy.core.system.action;

import ge.core.system.entity.Entity;
import ge.core.system.entity.Mapping;

import java.util.LinkedList;
import java.util.List;

import com.fzy.core.system.service.DbService;
import com.fzy.core.utils.NumberUtils;
import com.fzy.core.utils.U;
import com.fzy.core.webwork.BaseAction;
import com.fzy.core.webwork.MyValidationAware;
import com.opensymphony.xwork.ModelDriven;
import com.opensymphony.xwork.Preparable;

public class AddTable2mappingAction extends BaseAction implements Preparable, MyValidationAware, ModelDriven {
	private static final long serialVersionUID = 4031874703895594415L;
	String errorDispatch;
	DbService dbService;
	List tableNames;
	Mapping mapping;
	Entity defaultEntity;

	public void prepare() throws Exception {
		errorDispatch = "redirect:list.do";
		tableNames = new LinkedList();
		String mappingIdString = request.getParameter("id");
		Long mappingId = NumberUtils.createLongSafely(mappingIdString);
		if(mappingId != null)
			mapping = (Mapping)U.getBaseService().get(Mapping.class, mappingId);
		if(mapping == null)
			mapping = new Mapping();
		defaultEntity = new Entity();
	}
	
	public String execute() throws Exception {
		
		dbService.addTable2mapping(mapping, defaultEntity, tableNames);
		addActionMessage(getText("success.save"));
		return "redirect:input.do?id="+ mapping.getId();
	}

	public String getErrorDispatch() {
		return errorDispatch;
	}

	public List getTableNames() {
		return tableNames;
	}

	public void setTableNames(List tableNames) {
		this.tableNames = tableNames;
	}

	public void setDbService(DbService dbService) {
		this.dbService = dbService;
	}

	public Entity getDefaultEntity() {
		return defaultEntity;
	}

	public Object getModel() {
		return mapping;
	}

}
